package com.example.androidmainshixun.view;

import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.androidmainshixun.R;

import java.util.ArrayList;

public class PublishActivity extends AppCompatActivity {

    private static final int REQUEST_IMAGE_PICK = 1;
    private ArrayList<String> selectedImages = new ArrayList<>();

    private EditText etContent;
    private Button btnSubmit;
    private ImageButton btnAddImage, btnBack;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish);

        etContent = findViewById(R.id.et_content);
        btnSubmit = findViewById(R.id.btn_submit);
        btnAddImage = findViewById(R.id.btn_add_image);
        btnBack = findViewById(R.id.btn_back);

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnAddImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                startActivityForResult(intent, REQUEST_IMAGE_PICK);
            }
        });

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = etContent.getText().toString();
                if (content.isEmpty()) {
                    Toast.makeText(PublishActivity.this, "内容不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                Intent resultIntent = new Intent();
                resultIntent.putExtra("content", content);
                resultIntent.putStringArrayListExtra("selectedImages", selectedImages);
                setResult(RESULT_OK, resultIntent);
                finish();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_PICK && resultCode == RESULT_OK && data != null) {
            if (data.getClipData() != null) {
                for (int i = 0; i < data.getClipData().getItemCount(); i++) {
                    ClipData.Item item = data.getClipData().getItemAt(i);
                    Uri imageUri = item.getUri();
                    String imagePath = getPathFromUri(imageUri);
                    selectedImages.add(imagePath);
                }
            } else {
                Uri imageUri = data.getData();
                String imagePath = getPathFromUri(imageUri);
                selectedImages.add(imagePath);
            }
        }
    }

    private String getPathFromUri(Uri uri) {
        // 实现从URI获取路径的逻辑
        // 根据实际情况调整
        return uri.getPath();
    }
}